<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		
	);
	$list=$this->get_list('zhaoshanglist',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['address']=pdo_getcolumn('core_area',array('code'=>$item['code']),'address');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'招商员管理'),
			array('op'=>'edit','title'=>'添加招商员'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'id','title'=>'招商ID','type'=>'string'),
			array('name'=>'realname','title'=>'名称','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'address','title'=>'招商地区','type'=>'string'),
			array('name'=>'level_id','title'=>'招商员等级','type'=>'select','options'=>'levels'),
			array('name'=>'status','title'=>'招商员状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'order_num','title'=>'招商订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'招商订单总金额','type'=>'string'),
			array('name'=>'stop','title'=>'禁用？','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
			array('type'=>'href','action'=>'jy_merch/zhaoshang.list/addmerch/','title'=>'添加商户'),
			array('type'=>'href','action'=>'jy_merch/zhaoshang.merchlist/','title'=>'商户管理'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'agent_status'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$agent_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('zhaoshang')->check_agent($agent_id,$status,$post['remark']);
		exi('审核成功！','','list');
	}
	$item=$this->get('zhaoshanglist',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定招商员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核区域代理申请',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'agent_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['agent_status']=$this->agent_status;
	exi($return);
}
if($_RQ['op']=='addmerch'){
	if($_RQ['postdata']){
		$post=get_postdata();	
		if(count($post['merchs'])==0){
			exi('请选择要绑定的商户！','error');
		}
		foreach($post['merchs'] as $mv){
			pdo_update('jy_merch_user',array('zhaoshangid'=>$post['id']),array('id'=>$mv['id']));
		}
		exi('添加商户成功！','','list');
	}
	$item=array('merchs'=>array(),'id'=>$_RQ['id']);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核区域代理申请',
		'edit'=>array(
			array('name'=>'merchs','title'=>'选择商户','type'=>'choose_many','action'=>'jy_merch/get_merch/many/'),
		),
	);
	$return['agent_status']=$this->agent_status;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_merch_zhaoshanglist',array('id'=>$id));
		$item['lat_lng']=array('lat'=>$item['lat'],'lng'=>$item['lng']);
	}else{
		$item=array();
	}
	$item['upass']='';
    $return['item']=$item;	
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'招商员设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'招商员管理'),
			array('op'=>'edit','title'=>'添加招商员'),
		),
		'edit'=>array(
			array('name'=>'realname','title'=>'招商员姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'mid','title'=>'绑定会员','type'=>'choose','action'=>'core/member.get_member/'),							
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'level_id','title'=>'等级','type'=>'select','options'=>'levels','option_title'=>'title'),
			array('name'=>'stop','title'=>'禁用?','type'=>'switch'),
		)
	);
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	$return['status']=array('0'=>'待审核','1'=>'审核通过','2'=>'审核不通过');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['realname'] || !$post['mid'] ||  !$post['mobile']){
		exi('招商员姓名，绑定会员和电话必填','error');
	}
	$data =array(
			'realname'=>$post['realname'],
			'mobile'=>$post['mobile'],
			'mid'=>$post['mid'],
			'level_id'=>$post['level_id'],
			'status'=>$post['status'],
			'stop'=>$post['stop'],
	);
	if($post['id']){
		pdo_update('jy_merch_zhaoshanglist',$data,array('id'=>$post['id']));
	}else{
		$this->model('zhaoshang')->register($data);
	}	
	exi('保存成功','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	pdo_update('jy_merch_zhaoshanglist',array('stop'=>intval($post['stop'])),array('id'=>$id));	
	exi('设置成功！');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('zhaoshanglist',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('zhaoshanglist',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>